import { factory, sequelize } from '../models'
import { Router } from 'express'

let routes = Router()

routes.post('/', (req, res) => {

})

routes.get('/:id', (req, res) => {
  factory.findAll({
    raw: true,
    where: {
      id: req.params.id
    }
  }).then(factorys => {
    res.json(factorys)
  })
})

routes.get('/user/:id', (req, res) => {
  factory.findAll({
    raw: true,
    where: {
      user_id: req.params.id
    }
  }).then(factorys => {
    res.json(factorys)
  })
})


routes.get('/type/:type', (req, res) => {
  let sql = 'select f.*, u.phone from factories as f left join users as u on f.user_id = u.id where f.type =?'
  sequelize.query(sql, {
    type: sequelize.QueryTypes.SELECT,
    replacements: [req.params.type]
  }).then(factorys => {
    res.json(factorys)
  })

})
export default routes
